#!/bin/bash
#*/1 * * * * sh /application/bin/application.sh check >/dev/null 2>&1 &
source ~/.bash_profile

#############获取当前脚本层目录#########
sbin=`dirname "${BASH_SOURCE-$0}"`
echo ${sbin}
sbin=`cd "$sbin"; pwd`
echo ${sbin}
HOME_DIR="$sbin"/..

url=${HOME_DIR}/bin
path=${HOME_DIR}/lib
logHome=${HOME_DIR}/logs

mainClass=com.model.cn.main

size="-Xmx8192m -Xms4096m"

crontab_log=${home}/logs/crontab.log
  
function start() {
	checkVersion
	ID=`ps -ef|grep "$mainClass" |grep -v "$0" |grep -v "grep" |awk '{print $2}'`
	if [ -n "$ID" ];then
		echo running as process $ID.  Stop it first.
        exit 1
	fi
	
    jar_dir=$(cd ${path}; pwd)
    for i in `ls $jar_dir`
    do
	   if [[ $i != *.jar ]]; then
           continue
       fi
       if [ ${cppath} ]; then
           cppath=$cppath:$jar_dir/$i
       else
           cppath=$jar_dir/$i
       fi
    done
	
	#0 test 
	if [[ $1 == 0 ]] ; then 
	    echo "test mode"
        java ${size} -cp ${cppath} ${mainClass}
		# spring boot
		#java ${size} -DLOG_HOME=${home} -DLOG_FILE_NAME="" -Dspring.config.location=${home}/config/application.yml -Dlogging.config=${home}/config/logback.xml -jar ${home}/lib/${mainClass} 
	
	else
		#-DLOG_HOME=${home} -Dlogging.config=${home}/config/logback.xml
	    nohup java ${size}  -cp ${cppath} ${mainClass}  >/dev/null  2>&1 &
		# spring boot
		#nohup java ${size} -DLOG_HOME=${home} -DLOG_FILE_NAME="" -Dspring.config.location=${home}/config/application.yml -Dlogging.config=${home}/config/logback.xml -jar ${home}/lib/${mainClass} >/dev/null 2>&1 &
	    echo "ok"
	fi
    
}  

function stop() { 
   ID=`ps -ef|grep "${mainClass}" |grep -v "$0" |grep -v "grep" |awk '{print $2}'`
   if [ -n "$ID" ];then
	for one in `ps -ef|grep "${ID}" |grep -v "$0" |grep -v "grep" |awk '{print $2}'`
        do
            kill -9 $one
	        echo "Kill Old Run:"$one
        done
   fi
}  
  
function restart() {  
    stop
    sleep 1
    start
} 
function state() {
   ps -ef|grep ${mainClass} |grep -v "grep"
}


#java 版本效验
versionSize="1.8"
function checkVersion() {
 if type -p java >/dev/null; then
     #echo found java executable in PATH
     _java=java
 elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
     #echo found java executable in JAVA_HOME
     _java="$JAVA_HOME/bin/java"
 else
     echo "no java"
     exit 1
 fi

 if [[ "$_java" ]]; then
     version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
     if [[ "$version" < "$versionSize" ]]; then
         echo Java Version ${version} Less Than Need ${versionSize}
         exit 1
     fi
 fi

}

function check() { 
   ID=`ps -ef|grep "${mainClass}" |grep -v "$0" |grep -v "grep" |awk '{print $2}'`
   if [ -z "$ID" ];then
        start 1
		echo "$(date +%Y-%m-%d" "%H:%M):restart ${command} " >> ${crontab_log}
   fi
   return 0
}  
  
case "$1" in
	test )
		start 0
	;;
    state )
		state
	;;  
    start )  
        start 1
        ;;  
    stop )  
        stop  
        ;;
    check )  
        check  
        ;;
    restart )  
        restart  
        ;;  
    * )  
        echo "No $1 Command,Only Suport \"start,stop,restart,test\""  
        ;;  
esac 